package yf.platform.biz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import yf.platform.bo.BizResult;


public class ModelService extends Service {

	public BizResult list(Map<String,String> input){
		rst.setReqAttribute("models", this.daoSupport.find("sys_model",null,null,null));
		return rst.setCode(0)
				.setType(BizResult.REDIRECT)
				.setPage("WIDGETS/MODEL/model.jsp");
	}
	
	public BizResult field(Map<String,String> input){
		return rst.setCode(0)
				.setReqAttribute("fields", this.daoSupport.desc(input.get("t")))
				.setType(BizResult.REDIRECT)
				.setPage("WIDGETS/MODEL/field.jsp");
	}
	public BizResult removerelation(Map<String,String> input){
		String m_id = input.get("m_id");
		this.daoSupport.delete("sys_model_control", "mc_model_id = "+m_id);
		this.daoSupport.delete("sys_model", "m_id = "+m_id);
		if(isajax)
			return rst.setCode(0);
		return list(input);
			
	}
	
	public BizResult modifyrelation(Map<String,String> input){
		String mc_model_id = input.get("mc_model_id");
		String mc_attr_name = input.get("mc_attr_name");
		this.daoSupport.update(input, "mc_model_id = "+mc_model_id+" and mc_attr_name = '"+mc_attr_name+"'", "sys_model_control");
		if(isajax)
			return rst.setCode(0);
		return list(input);
	}
	
	public BizResult addreleation(Map<String,String> input){
		Map<String,String> e = new HashMap<String,String>();
		e.putAll(input);
		String mid = e.get("mc_model_id");
		this.daoSupport.insert( "sys_model_control",e);
		e.put("m_relation","1");
		this.daoSupport.update(e, "m_id = "+mid, "sys_model");
		if(isajax){
			return rst.setCode(0);
		}
		return list(input);
	}
	
	public BizResult control(Map<String,String> input){
		Map<String,Object> model = this.daoSupport.findOne("sys_model","m_id,m_name,m_title,m_view", "m_id = "+input.get("id"));
		//加载关联信息
		 List<Map<String, Object>> fields = this.daoSupport.find("sys_model_control",null,"mc_auto = 0 and mc_model_id="+model.get("m_id"),null);
		rst.setReqAttribute("fields", fields);
		rst.setReqAttribute("model", model);
		//加载系统控件
		List<Map<String,Object>> controls = this.daoSupport.find("sys_control", null, " c_enable = 1",null);
		rst.setReqAttribute("options", controls);
		//加载数据源
		List<Map<String,Object>> ds = this.daoSupport.find("sys_ds", null, null,null);
		
		rst.setReqAttribute("ds", ds);
		return rst.setCode(0).setType(BizResult.REDIRECT)
				.setPage("WIDGETS/MODEL/relation.jsp");
	}
	
	public BizResult table(Map<String,String> input){
		return rst.setCode(0).setType(BizResult.REDIRECT)
			.setReqAttribute("tables", this.daoSupport.table())
			.setPage("WIDGETS/MODEL/table.jsp");
	}
	
	/**
	 * 抽取模型的操作
	 * 1.添加模型
	 * 2.直接关联字段
	 * 
	 * @param input
	 * @return
	 */
	public BizResult add(Map<String,String> input){
		//添加模型
		int id = this.daoSupport.insert( "sys_model",input);
		
		if(id >0){
			List<Map<String,String>> es = new ArrayList<Map<String,String>>();
			Map<String,String> e;
			List<Map<String,Object>> view_fields = this.daoSupport.desc(input.get("m_view"));
			for(Map<String,Object> field : view_fields){
				e = new HashMap<String,String>();
				e.put("mc_control_id", "2");
				e.put("mc_auto", "auto_increment".equals(field.get("Extra"))?"1":"0");
				e.put("mc_pri", "PRI".equals(field.get("Key"))?"1":"0");
				e.put("mc_null", "YES".equals(field.get("Null"))?"1":"0");
				e.put("mc_attr_name",field.get("Field").toString());
				e.put("mc_prompt",field.get("Comment").toString());
				e.put("mc_model_id",String.valueOf(id));
				es.add(e);
			}
			this.daoSupport.insert( "sys_model_control",es.toArray(new Map[]{}));
			return isajax?rst.setCode(0):list(input);
		}
		return rst.setCode(-101).setMsg("DB ACCESS ERROR!");
	}

	public BizResult del(Map<String,String> input){
		return rst.setCode(0).setType(BizResult.REDIRECT)
				.setPage("WIDGETS/MODEL/add.jsp");
	}
	
	public BizResult addform(Map<String,String> input){
		String t = input.get("t");
		return rst.setCode(0).setReqAttribute("table", t)
			.setType(BizResult.REDIRECT)
			.setPage("WIDGETS/MODEL/add.jsp");
	}
	
	public BizResult updform(Map<String,String> input){
		//TODO..获取值
		return rst.setCode(0)
				.setType(BizResult.REDIRECT)
				.setPage("WIDGETS/MODEL/upd.jsp");
	}
}
